//
//  BrokerDM.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/21.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "BrokerDM.h"
#import "API.h"
#import "SingletonData.h"
#import "StringTool.h"
#import "NearbyCellData.h"

#import "UserModal.h"
#import "StoreInfoModel.h"
#import "JSONKit.h"


@interface BrokerDM ()
@property (nonatomic,assign) int pageIndex;
@property (copy, nonatomic) NSString *anencyCode;
@end

@implementation BrokerDM
@synthesize pageIndex;



#pragma mark 获取经纪人列表
-(ResulteMod*)refreshDataWithStoreCode:(NSString *)storeCode withKeyWord:(NSString *)keyword{
    
    pageIndex = 1;
   NSString *url = [NSString stringWithFormat:@"%@%@/%zd/8",[API urlMake:BrokerList],keyword,1];;

    
    NSDictionary *dic;
    if ([StringTool isBlankString:storeCode]) {
        
        dic =  @{@"isBrokerageShow":@"1",@"agencyCode":[UserModal shareUserModel].userCode};
    }else{
        dic =  @{@"storeCode":storeCode,@"isBrokerageShow":@"1",@"agencyCode":[UserModal shareUserModel].userCode};
    }
    

    self.anencyCode = [UserModal shareUserModel].userCode;
   
   ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dic];
    mod.resultCode = [NSString stringWithFormat:@"%@",[mod.resultDic objectForKey:@"code"] ];
    
    if (![mod.resultCode isEqualToString:@"20003"] && ![mod.resultCode isEqualToString:@"0"])
    {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        return mod;
        
    }
    
    NSArray *brokersDtosArr = mod.resultDic[@"brokersDtos"];
    
    NSMutableArray *mainArr = [NSMutableArray array];
    for (NSDictionary *dic in brokersDtosArr)
    {
        BorkerCellModel *data = [BorkerCellModel borkderCellModelWithDic:dic];
        [mainArr addObject:data];
    }
    
    
    if (mainArr.count>0) {
        pageIndex++;
    }
    

    mod.dataMod = mainArr;
    
    return mod;
}


-(ResulteMod*)refreshDataWithAgencyCode2:(NSString *)agencyCode withKeyWord:(NSString *)keyword{

    
    pageIndex = 1;
    NSString *url = [NSString stringWithFormat:@"%@%zd/8",[API urlMake:BrokerList],pageIndex];
    NSDictionary *dic =  @{@"agencyCode":agencyCode,@"keyword":keyword};
    
    
    NSLog(@"------j获取经济人列表%@",url);
    self.anencyCode = agencyCode;
    
    ResulteMod *mod = [Connect doPostCaCheWithUrl:url withParamDic:dic];
    mod.resultCode = [NSString stringWithFormat:@"%@",[mod.resultDic objectForKey:@"code"] ];
    
    if (![mod.resultCode isEqualToString:@"20003"] && ![mod.resultCode isEqualToString:@"0"])
    {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        return mod;
        
    }
    
    NSArray *brokersDtosArr = mod.resultDic[@"brokersDtos"];
    
    NSMutableArray *mainArr = [NSMutableArray array];
    for (NSDictionary *dic in brokersDtosArr)
    {
        BorkerCellModel *data = [BorkerCellModel borkderCellModelWithDic:dic];
        [mainArr addObject:data];
    }
    
    
    if (mainArr.count>0) {
        pageIndex++;
    }
    
    
    mod.dataMod = mainArr;
    
    return mod;



}


#pragma mark  经纪人列表加载更多
-(ResulteMod *)loadMoreDataWithKeyword:(NSString *)keyword withIndex:(NSInteger )pageInd;
{
    
   NSString *url = [NSString stringWithFormat:@"%@%@/%zd/8",[API urlMake:BrokerList],keyword,pageInd];
    
    NSDictionary *dic =  @{@"agencyCode":self.anencyCode};
    
   ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dic];;
    mod.resultCode = [NSString stringWithFormat:@"%@",[mod.resultDic objectForKey:@"code"] ];
    
    if (![mod.resultCode isEqualToString:@"20003"] && ![mod.resultCode isEqualToString:@"0"])
    {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        return mod;
        
    }
    
    NSArray *brokersDtosArr = mod.resultDic[@"brokersDtos"];
    
    NSMutableArray *mainArr = [NSMutableArray array];
    for (NSDictionary *dic in brokersDtosArr)
    {
        BorkerCellModel *data = [BorkerCellModel borkderCellModelWithDic:dic];
        [mainArr addObject:data];
    }
    
    
    if (mainArr.count>0) {
        pageIndex++;
    }
    
    
    mod.dataMod = mainArr;
    
    return mod;
    

}



#pragma mark 选择门店--的列表
-(ResulteMod*)refreshStoresDatawithStorecode:(NSString *)storeCode{
    
    NSString *code = [StringTool isBlankString:[UserModal shareUserModel].userCode]?storeCode:[UserModal shareUserModel].userCode;
    NSString *url = [NSString stringWithFormat:@"%@%@",[API urlMake:StoreList],code];

    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    mod.resultCode = [NSString stringWithFormat:@"%@",[mod.resultDic objectForKey:@"code"] ];
    if (![mod.resultCode isEqualToString:@"20003"] && ![mod.resultCode isEqualToString:@"0"])
    {
        mod.resltState = ResultFail;
        return mod;
        
    }
    
    NSMutableArray *array = [[NSMutableArray alloc]init];
    
    NSArray *mainArray = mod.resultDic[@"brokerListDtos"];
    
    for (NSDictionary *itemDic in mainArray) {
        NearbyCellData *cellData  = [[NearbyCellData alloc]initWithTitle:itemDic[@"storeName"] WithDetail:itemDic[@"storeCode"] WithMoreDetail:nil];
        
        
        [array addObject:cellData];
    }
    
    mod.dataMod = array;
    

    return mod;
}

#pragma mark 单个修改经纪人状态（启用，禁用）stauts:0进行启用操作，1进行禁用操作
-(ResulteMod *)modifyuserstatusWithModel:(BorkerCellModel *)model
{
    NSString *url = [NSString stringWithFormat:@"%@",[API urlMake:modifyuserstatus]];
    NSString *consumerId  = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    NSString *status = model.status == 0?@"1":@"0";
    
    NSDictionary *dict = @{@"brokerId":model.brokerId,@"status":status,@"consumerId":consumerId};
                           
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        mod.resltState = ResultFail;
        
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
                           
    return mod;
}
#pragma mark 单个操作，经纪人移动
-(ResulteMod *)changeshopWithBorkerId:(NSString *)brokerId withShopCode:(NSString *)shopCode;
{
    NSString *url = [NSString stringWithFormat:@"%@",[API urlMake:changeshop]];
    
    NSString * consumerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    NSDictionary *dict = @{@"brokerId":brokerId,@"shopCode":shopCode,@"consumerId":consumerId};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];

    mod.resultCode =  [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    mod.resultTip = mod.resultDic[@"message"];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    return mod;
    
}

#pragma mark 添加门店
-(ResulteMod *)createshopWith:(NSString *)shopName;
{
    NSString *url = [API urlMake:createshop];
    NSDictionary *dict = @{@"shopName":shopName,@"parentCode":[UserModal shareUserModel].userCode};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    return mod;
    
    
}

#pragma mark 添加经纪人
-(ResulteMod *)createbrokerWithAddBrokerModel:(AddBrokerModel *)model
{
    NSString *url = [API urlMake:createbroker];

    NSString *password = [NSString md5:model.password];
    
    
    NSDictionary *dict = @{@"name":model.name,@"password":password,@"captcha":model.captha,@"repassword":password,@"mobile":model.mobile,@"parentCode":model.parentCode};
    ResulteMod *mod = [Connect doPostOutCaCheWithUrl:url withParamDic:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
       
        mod.resltState = ResultFail;
        
        if ([mod.resultCode isEqualToString:@"30002"]) {
            mod.resultTip = @"验证码错误";
        }else
        {
            mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        }

    }
    return mod;
    
}

#pragma mark 门店管理－－获取门店列表
-(ResulteMod *)storemanagelistrefreshData;
{
    NSString *url = [NSString stringWithFormat:@"%@%@",[API urlMake:storemanagelist],[UserModal shareUserModel].userCode];
    
    ResulteMod *mod = [Connect doGetWithOutCaChe:url];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    
    if (![mod.resultCode isEqual:@"20003"] && ![mod.resultCode isEqual:@"0"]) {
        mod.resltState = ResultFail;

       mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
        return mod;
    }
    
    NSArray *resArr = [[NSArray alloc]initWithArray:mod.resultDic[@"manageListDtos"]];
    
    NSMutableArray *mainArrM = [NSMutableArray array];
    
    for (NSDictionary *dict in resArr) {
        
        StoreInfoModel *model = [StoreInfoModel storeInfoModelWithDict:dict];
        [mainArrM addObject:model];
    }
    
    mod.dataMod = mainArrM;
    return mod;
    
}


#pragma mark  批量门店启用禁用操作 stauts 0 要进行启用操作，1禁用操作
-(ResulteMod *)batchmodifystorestatusWithStoreId:(NSString *)storeId WithStatus:(NSString *)stauts;
{
    NSString *url = [API urlMake:batchmodifystorestatus];
    NSString *coutomerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    
    NSDictionary *dict = @{@"consumerId":coutomerId,@"storeIds":storeId,@"status":stauts};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    
    return mod;
    
}

#pragma mark 门店启用，禁用（单个）
-(ResulteMod *)modifystorestatusWithStoreId:(NSString *)storeId WithStatus:(NSString *)status
{
    NSString *url = [API urlMake:modifystorestatus];
    
    NSString *consumerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    NSDictionary *dict = @{@"consumerId":consumerId,@"storeId":storeId,@"status":status};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
       mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    return mod;
}

#pragma mark 经纪人 批量操作（启用，禁用）
-(ResulteMod *)batchmodifyuserstatusWithBorkerId:(NSString *)brokerId withStatuts:(NSString *)status;
{
    NSString *url = [API urlMake:batchmodifyuserstatus];
    
    NSString *consumerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    NSDictionary *dict = @{@"consumerId":consumerId,@"brokerIds":brokerId,@"status":status};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
       mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    
    return mod;
}

#pragma mark 经纪人批量移动门店
-(ResulteMod *)batchchangeshopWithBrokerIds:(NSString *)brokerIds WithShopCode:(NSString *)shopCode;
{
    NSString *url = [API urlMake:batchchangeshop];
    
     NSString *consumerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    NSDictionary *dict = @{@"brokerIds":brokerIds,@"shopCode":shopCode,@"consumerId":consumerId};
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    mod.resultCode = [NSString stringWithFormat:@"%@",mod.resultDic[@"code"]];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    
    return mod;

}

#pragma mark 经纪人
-(ResulteMod *)modifybrokerWithBrokerId:(NSString *)brokerId WithBrokerPhone:(NSString *)phone withbrokerName:(NSString *)brokerName;
{
    NSString *url = [API urlMake:modifybroker];
    NSString *coustomerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    NSDictionary *dict = @{@"consumerId":coustomerId,@"brokerId":brokerId,@"brokerName":brokerName,@"brokerMobile":phone};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
        mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }
    
    return mod;
    
}

#pragma mark门店编辑
-(ResulteMod *)modifystoreWithStoreId:(NSString *)storeId withStoreName:(NSString *)storeName;
{
    NSString *url = [API urlMake:modifystore];
    
    NSString *coustomerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
    
    NSDictionary *dict = @{@"consumerId":coustomerId,@"storeId":storeId,@"storeName":storeName};
    
    ResulteMod *mod = [Connect doPostWithOutCaCheWithUrl:url withFormdata:dict];
    if (![mod.resultCode isEqualToString:@"20003"]) {
        
        mod.resltState = ResultFail;
       mod.resultTip  = ![StringTool isBlankString:mod.resultDic[@"message"]]?mod.resultDic[@"message"]:[StringTool isBlankString:mod.resultCode]?@"网络错误":[NSString stringWithFormat:@"网络错误%@",mod.resultCode];
    }

    return mod;
}

@end
